package com.thinksns.sociax.t4.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.fragment.FragmentSociax;
import com.thinksns.sociax.t4.component.HolderSociax;
import com.thinksns.sociax.t4.exception.VerifyErrorException;
import com.thinksns.sociax.t4.model.ModelComment;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.activity.widget.EmptyLayout;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;
import com.thinksns.sociax.thinksnsbase.exception.ApiException;
import com.thinksns.sociax.thinksnsbase.exception.DataInvalidException;
import com.thinksns.sociax.thinksnsbase.exception.ExceptionIllegalParameter;
import com.thinksns.sociax.thinksnsbase.exception.ListAreEmptyException;

/**
 * Created by ZYCX on 2016/10/30.
 */
public class AdapterPostDetail extends AdapterCommentList {
    public AdapterPostDetail(ThinksnsAbscractActivity context, ListData<SociaxItem> list, int feed_id) {
        super(context, list, feed_id);
    }

    public AdapterPostDetail(FragmentSociax fragment, ListData<SociaxItem> list, int feed_id) {
        super(fragment, list, feed_id);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        HolderSociax holder = null;
        int type = getItemViewType(position);
        if (convertView == null || convertView.getTag(R.id.tag_viewholder) == null) {
            if (type == 1) {
                convertView = inflater.inflate(R.layout.listitem_comment_list, null);
                holder = append.initHolder(convertView);
            } else if (type == 0 || type == 2) {
                convertView = new EmptyLayout(parent.getContext());
                ListView listView = (ListView) parent;
                int width = listView.getWidth();
                int height = listView.getHeight();
                int count = listView.getHeaderViewsCount();
                int headerH = 0;
                for (int i = 0; i < count; i++) {
                    if (listView.getChildAt(i) != null)
                        headerH += listView.getChildAt(i).getBottom();
                }

                height -= headerH;
                int minHeight = UnitSociax.dip2px(parent.getContext(), 100);
                if (height < minHeight) {
                    //设置最小高度
                    height = minHeight;
                }

                AbsListView.LayoutParams params = new AbsListView.LayoutParams(width, height);
                convertView.setLayoutParams(params);
                ((EmptyLayout) convertView).setNoDataContent(parent.getContext().getResources().getString(R.string.empty_user_comment));
            }

            convertView.setTag(R.id.tag_viewholder, holder);

        } else {
            holder = (HolderSociax) convertView.getTag(R.id.tag_viewholder);
        }

        if (type == 1) {
            ModelComment comment = (ModelComment) getItem(position);
            if (comment.getComment_id() > 0) {
                holder.ll_empty.setVisibility(View.GONE);
                holder.ll_content.setVisibility(View.VISIBLE);
                convertView.setTag(R.id.tag_object, comment);
                holder.tv_comment_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ListView) parent).performItemClick(v, position, position);
                    }
                });
                append.appendCommentData(holder, comment);
            } else {
                if (getCount() == 1) {
                    holder.ll_content.setVisibility(View.GONE);
                    holder.ll_empty.setVisibility(View.VISIBLE);
                }else{
                    holder.ll_content.setVisibility(View.GONE);
                    holder.ll_empty.setVisibility(View.GONE);
                }
            }
        } else if (type == 2) {
            //加载中内容
            ((EmptyLayout) convertView).setErrorType(EmptyLayout.NETWORK_LOADING);
        } else {
            ((EmptyLayout) convertView).setErrorType(EmptyLayout.NODATA);
        }

        return convertView;
    }

    @Override
    public ListData<SociaxItem> refreshNew(int count) throws VerifyErrorException, ApiException, ListAreEmptyException, DataInvalidException {
        ListData<SociaxItem> itemListData = null;
        try {
            itemListData = getApi().getPostCommentList(10, feed_id, 0, httpListener);

        } catch (ExceptionIllegalParameter exceptionIllegalParameter) {
            exceptionIllegalParameter.printStackTrace();
        } finally {
            if (itemListData == null) {
                itemListData = new ListData<>();
            }
            if (itemListData.isEmpty()) {
                itemListData.add(new ModelComment());
            }
            return itemListData;
        }
    }

    @Override
    public ListData<SociaxItem> refreshFooter(SociaxItem obj) throws VerifyErrorException, ApiException, ListAreEmptyException, DataInvalidException {
        ListData<SociaxItem> itemListData = null;
        try {
            itemListData = getApi().getPostCommentList(10, feed_id, getMaxid(), httpListener);

        } catch (ExceptionIllegalParameter exceptionIllegalParameter) {
            exceptionIllegalParameter.printStackTrace();
        } finally {
            if (itemListData == null) {
                itemListData = new ListData<>();
            }
            if (itemListData.isEmpty()) {
                itemListData.add(new ModelComment());
            }
            return itemListData;
        }
    }

    @Override
    public void addHeader(ListData<SociaxItem> list) {

        if (list == null)
            list = new ListData<SociaxItem>();

        if (list.size() > 0) {
            //过滤重复信息 修改下去重方式
            for (int i = 0; i < list.size(); i++) {
                if (this.list.contains(list.get(i))) {
                    list.remove(i);
                    i--;
                }
            }
            if (!list.isEmpty()) {
                this.list.clear();
            }
            this.list.addAll(0, list);
            this.notifyDataSetChanged();

        } else {
            if (!isHideFootToast)
                Toast.makeText(context, com.thinksns.sociax.android.R.string.refresh_error, Toast.LENGTH_SHORT).show();
            adapterState = NO_NEW_DATA;
        }

        if (fragment != null) {
            fragment.executeDataSuccess(list);
        }

        if (context != null)
            context.executeDataSuccess(list);
    }
}
